export class DMessageClient {
  constructor(httpServerAddr, httpProxyPath, messageHandler) {
    if (typeof Worker === "undefined") {
      return null;
    } else {
      this.worker = new SharedWorker('DMessageClientsSharedWorker.js?httpServerAddr=' + httpServerAddr + '&httpProxyPath=' + httpProxyPath);
      if (typeof messageHandler === 'function') {
        this.worker.port.addEventListener('message', messageHandler, false);
        this.worker.port.start();//open the port connection
      }
    }
  }
  /*
  * sendMessage: 发送操作消息
  * message是一个类：{message,currNo,groupNoInCourse,taskNoInGroup，guidelineNoInTask}
  * 类中的message是消息的内容，比如：ＸＸＸ开始学习任务ＸＸＸ；老师评阅了ＸＸＸ，９０分。
  * 其类的message、currNo是必须的，其余可选
  */
  sendMessage(message) {
    this.worker.port.postMessage({ eventName: "reportAction", message });
  }

  /*
  *  需要或不需要某个课程的消息
  *  默认是不需要（不发送消息过来） 
  *  发出去的操作消息，会发送给那些需要的网页
  */


  needNotMessageOf(currNo) {
    this.worker.port.postMessage({ eventName: "leaveRoom", message: { currNo } });
  }

  needMessageOf(currNo) {
    this.worker.port.postMessage({ eventName: "joinRoom", message: { currNo } });
  }

  closing() {
    this.worker.port.postMessage({ eventName: "portClosing", messge: {} })
  }

}